package com.example.springmvc_demo3.controller;

import com.example.springmvc_demo3.dto.UserDTO;
import com.example.springmvc_demo3.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AuthController {
    @Autowired
    private UserService userService;
    //打开注册页面
    @GetMapping("/register")
    public String openRegisterPage(){
        return "register";
    }
    //处理注册请求
    @PostMapping("/register")
    public ModelAndView register(UserDTO userDTO, ModelAndView mv){
        //处理异常，如果注册失败，会有异常抛出，需要处理
        try {
            userService.register(userDTO);
            mv.addObject("msg","注册成功");
        }catch (Exception e){
            mv.addObject("msg","账户已存在");
        }
        mv.setViewName("register");
        return mv;
    }

}